package com.hopes.bizz.enforcement.integral.listener;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.IService;
import com.hopes.bizz.enforcement.integral.bo.IntegralEvent;
import com.hopes.bizz.enforcement.integral.entity.HazIntegralEventEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author yumr
 * @Description 积分事件处理接口
 * @time 2023/8/17 18:06
 */
public interface IntegralEventHandler<T> {

    /**
     * 接口服务
     * @return 接口服务
     */
    IService<T> getService();

    /**
     * 事件处理
     * @param integralEvent 事件
     */
    void handler(IntegralEvent integralEvent);

    /**
     * 事件处理服务
     * @return 接口服务
     */
    IService<HazIntegralEventEntity> getEventService();

    default Map<Integer, Integer> integralEventMap() {
        final List<HazIntegralEventEntity> eventList = getEventService().list();
        if (CollUtil.isEmpty(eventList)) {
            return new HashMap<>();
        }
        return eventList.parallelStream().filter(e ->
                Objects.nonNull(e.getEventName())).collect(Collectors.toMap(HazIntegralEventEntity::getEventName, HazIntegralEventEntity::getIntegral));
    }

    default boolean saveIntegralRecordBatch(List<T> entityList) {
        return this.getService().saveBatch(entityList);
    }

}
